﻿<?php
$login = false;
$wrong = false;
if (isset($_SESSION['login'])) {
	$login = Member::load($_SESSION['login']);
} else {
	if (isset($_COOKIE['usuloginid']) && isset($_COOKIE['usutoken'])) {
		$member = Member::loginCookie($_COOKIE['usuloginid'], $_COOKIE['usutoken']);
		if ($member) {
			$_SESSION['login'] = $member->id;
			$login = $member;
		}
		else {
			setcookie("usuloginid", "", time()-60*60*24*60, "/");
			setcookie("usutoken", "", time()-60*60*24*60, "/");
		}
	}
	if (!$login && isset($_POST['mail']) && isset($_POST['password'])) {
		$member = Member::login($_POST['mail'], $_POST['password'], isset($_POST['cookie']));
		if ($member) {
			$_SESSION['login'] = $member->id;
			$login = $member;
		} else
			$wrong = true;
	}
}
if (!$login) {
	require("login.php");
	die();
}
?>